<?php

class Plugins extends SocialMashup {

	private $cachedir = "./";

	function __construct() {
		echo ("Plugin base class");
	}
	
	function toString() {
		echo "this is the class to string...";
	}
	
	function query_params($params) {
		
		$querystring = array();
	
		foreach ($params as $key => $val)
			array_push($querystring, $key . "=" . urlencode($val));
		
		return implode("&", $querystring);
		
	}
	
	function load_xml($baseurl, $query) {
		

		
		$file = $this->get_file($baseurl . $query, $query);
		/*
		$xmlDoc = new DOMDocument();
		$xmlDoc->loadXML($file);
		*/
		
		$xmlDoc = new SimpleXMLElement($file);
		return $xmlDoc;
		
	}
	
	function get_file($url, $cachekey)
	{
		
		$cachekey = str_replace("&", "", $cachekey);
		$cachekey = str_replace("=", "", $cachekey);
		$cachekey = str_replace(",", "_", $cachekey);

		// Caching using simple file swap cache. PHP4/PHP5
		// Caching code by Rasmus Lerdorf
		// February 1, 2006
		// Note: If you are using PHP 5.1 or newer, you can save
		// a system call by replacing time() with SERVER['REQUEST_TIME']

		// The ./ location puts the file in the root of your
		// web server,  usually

		$request =  $url;

		// If you make different types of requests, you'll want to implement
		// some sort of mapping from the request to unique filenames.
		$cache_filename = $cachekey;
		$cache_fullpath = $this->cachedir . $cache_filename;

		// Number of seconds until the cache gets stale
		$cache_timeout = 900;

		// Check the cache
		$response = $this->request_cache($request, $cache_fullpath, $cache_timeout);

		if ($response === false) {
			die('Request failed');
		}

		return $response;

		
	}
	
	function request_cache($url, $dest_file="", $timeout=900) {

		if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {

			$ch = curl_init();	
			curl_setopt($ch, CURLOPT_URL, $url);	
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
			$data = curl_exec($ch);		
			curl_close($ch);

			if ($data === false) return false;

			$tmpf = tempnam('/tmp','YWS');
			$fp = fopen($tmpf,"w");
			fwrite($fp, $data);
			fclose($fp);
			rename($tmpf, $dest_file);

		} else {

			return file_get_contents($dest_file);

		}

		return($data);
	}
	
	function create_socialmash_object($item)
	{
		parent::create_general_object($item);
	}
	
	function get_node($tag, $entry) {	
		$node = $entry->getElementsByTagName($tag);
		return $node->item(0)->nodeValue;
	}
	
	function get_attribute($entry, $attr) {	
		return $entry->getAttribute($attr);
	}
	
	function get_attribute_in_tag($tag, $entry, $attr) {
		$node = $entry->getElementsByTagName($tag);
		return $node->item(0)->getAttributeNode($attr)->value;
	}
	
	function get_by_attribute($tag, $entry, $attr, $required_attr, $required_val) {
		
		$nodes = $entry->getElementsByTagName($tag);
		foreach ($nodes as $node) {
			
			if ($node->getAttribute($required_attr) == $required_val)
				return $node->getAttribute($attr);
			
		}		
		
	}
	
	function toJson($obj)
	{
		echo(json_encode($obj));
	}
	
}

?>